package cn.iocoder.yudao.module.system.controller.admin.authrecorddatascopevalue;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.authrecorddatascopevalue.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.authrecorddatascopevalue.AuthRecordDataScopeValueDO;
import cn.iocoder.yudao.module.system.service.authrecorddatascopevalue.AuthRecordDataScopeValueService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 授权记录和权限范围值关系")
@RestController
@RequestMapping("/system/auth-record-data-scope-value")
@Validated
public class AuthRecordDataScopeValueController {

    @Resource
    private AuthRecordDataScopeValueService authRecordDataScopeValueService;

    @PostMapping("/create")
    @Operation(summary = "创建授权记录和权限范围值关系")
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:create')")
    public CommonResult<Long> createAuthRecordDataScopeValue(@Valid @RequestBody AuthRecordDataScopeValueSaveReqVO createReqVO) {
        return success(authRecordDataScopeValueService.createAuthRecordDataScopeValue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新授权记录和权限范围值关系")
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:update')")
    public CommonResult<Boolean> updateAuthRecordDataScopeValue(@Valid @RequestBody AuthRecordDataScopeValueSaveReqVO updateReqVO) {
        authRecordDataScopeValueService.updateAuthRecordDataScopeValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除授权记录和权限范围值关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:delete')")
    public CommonResult<Boolean> deleteAuthRecordDataScopeValue(@RequestParam("id") Long id) {
        authRecordDataScopeValueService.deleteAuthRecordDataScopeValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得授权记录和权限范围值关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:query')")
    public CommonResult<AuthRecordDataScopeValueRespVO> getAuthRecordDataScopeValue(@RequestParam("id") Long id) {
        AuthRecordDataScopeValueDO authRecordDataScopeValue = authRecordDataScopeValueService.getAuthRecordDataScopeValue(id);
        return success(BeanUtils.toBean(authRecordDataScopeValue, AuthRecordDataScopeValueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得授权记录和权限范围值关系分页")
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:query')")
    public CommonResult<PageResult<AuthRecordDataScopeValueRespVO>> getAuthRecordDataScopeValuePage(@Valid AuthRecordDataScopeValuePageReqVO pageReqVO) {
        PageResult<AuthRecordDataScopeValueDO> pageResult = authRecordDataScopeValueService.getAuthRecordDataScopeValuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuthRecordDataScopeValueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出授权记录和权限范围值关系 Excel")
    @PreAuthorize("@ss.hasPermission('system:auth-record-data-scope-value:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuthRecordDataScopeValueExcel(@Valid AuthRecordDataScopeValuePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuthRecordDataScopeValueDO> list = authRecordDataScopeValueService.getAuthRecordDataScopeValuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "授权记录和权限范围值关系.xls", "数据", AuthRecordDataScopeValueRespVO.class,
                        BeanUtils.toBean(list, AuthRecordDataScopeValueRespVO.class));
    }

}